using System;
using System.Data;
using PCSComUtils.PCSExc;
using PCSComUtils.MasterSetup.DS;
using PCSComProduction.ValueAddTransact.DS;
using PCSComUtils.Common;

namespace PCSComProduction.ValueAddTransact.BO
{
	public class MachineTimeBO //: IMachineTimeBO
	{
		public MachineTimeBO()
		{
			//
			// TODO: Add constructor logic here
			//
		}
		#region IObjectBO Members
		/// <summary>
		/// - Add new Machine Time Master to database and return new ID
		/// - Assign new MachineTimeMasterID for each Machine Time Detail in DataSet
		/// - Update detail DataSet
		/// </summary>
		/// <param name="pobjMachineMaster">Machine Time Master object</param>
		/// <param name="pdstDetailData">Machine Time Detail Data</param>
		/// <returns>New MachineTimeMasterID</returns>
		//[AutoComplete(true)]
		public int AddAndReturnID(object pobjMachineMaster, DataSet pdstDetailData)
		{
			try
			{
				//add the master first
				PRO_MachineTimeMasterDS objPRO_MachineTimeMasterDS = new PRO_MachineTimeMasterDS();
				int intMachineTimeMasterID = objPRO_MachineTimeMasterDS.AddAndReturnID(pobjMachineMaster);
				//update the id into the detail
				//update the dataset all of this id
				for (int i=0; i<pdstDetailData.Tables[0].Rows.Count;i++)
				{
					if (pdstDetailData.Tables[0].Rows[i].RowState != DataRowState.Deleted)
					{
						pdstDetailData.Tables[0].Rows[i][PRO_MachineTimeDetailTable.MACHINETIMEMASTERID_FLD] = intMachineTimeMasterID;
					}
				}
				
				//update the detail
				PRO_MachineTimeDetailDS objPRO_MachineTimeDetailDS = new PRO_MachineTimeDetailDS();
				objPRO_MachineTimeDetailDS.UpdateDataSet(pdstDetailData);

				//Get the data back in order to refresh the grid for later edit and delete

				return intMachineTimeMasterID;
			}			
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}
		/// <summary>
		/// Get all Machine Time detail data of Machine Time Master
		/// </summary>
		/// <param name="pintMachineTimeMasterID">Machine Time Master ID</param>
		/// <returns>All Machine Time detail data of Machine Time Master</returns>
		//[AutoComplete(true)]
		public DataSet GetDetailData(int pintMachineTimeMasterID)
		{
			try 
			{
				PRO_MachineTimeDetailDS objPRO_MachineTimeDetailDS = new PRO_MachineTimeDetailDS();
				return objPRO_MachineTimeDetailDS.GetDetailData(pintMachineTimeMasterID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}

		}
		/// <summary>
		/// Get Machine Time Master object from ID
		/// </summary>
		/// <returns>Machine Time Master</returns>
		//[AutoComplete(true)]
		public object GetMachineMasterObject(int pintMachineMasterID)
		{
			return null;
		}
		/// <summary>
		/// Get Master Location object from ID
		/// </summary>
		//[AutoComplete(true)]
		public object GetMasterLocationVO(int pintMasterLocationID)
		{
			try 
			{
				MST_MasterLocationDS objMST_MasterLocationDS = new MST_MasterLocationDS();
				return objMST_MasterLocationDS.GetObjectVO(pintMasterLocationID);
			}
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}

		}

		//[AutoComplete(true)]
		public void UpdateDataSet(System.Data.DataSet dstData)
		{
			// TODO:  Add MachineTimeBO.UpdateDataSet implementation
		}

		//[AutoComplete(true)]
		public void Update(object pObjectDetail)
		{
			// TODO:  Add MachineTimeBO.Update implementation
		}

		/// <summary>
		/// 1. Delete all Machine Time Detail by MachineTimeMasterID
		/// 2. Delete Machine Time Master object by MachineTimeMasterID
		/// </summary>
		/// <param name="pObjectVO"></param>
		//[AutoComplete(true)]
		public void Delete(object pObjectVO)
		{
			try
			{
				//delete the Detail first
				PRO_MachineTimeMasterVO objPRO_MachineTimeMasterVO = (PRO_MachineTimeMasterVO)pObjectVO;
				PRO_MachineTimeDetailDS objPRO_MachineTimeDetailDS = new PRO_MachineTimeDetailDS();
				objPRO_MachineTimeDetailDS.DeleteByMasterID(objPRO_MachineTimeMasterVO.MachineTimeMasterID);

				//delete the master
				PRO_MachineTimeMasterDS objPRO_MachineTimeMasterDS = new PRO_MachineTimeMasterDS();
				objPRO_MachineTimeMasterDS.Delete(objPRO_MachineTimeMasterVO.MachineTimeMasterID);
			

			}			
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		//[AutoComplete(true)]
		public void Add(object pObjectDetail)
		{
			// TODO:  Add MachineTimeBO.Add implementation
		}

		//[AutoComplete(true)]
		public object GetObjectVO(int pintID, string VOclass)
		{
			// TODO:  Add MachineTimeBO.GetObjectVO implementation
			return null;
		}

		/// <summary>
		/// Update data into database
		/// 1.Update the master
		/// 2.Update the detail
		/// </summary>
		/// <param name="pobjMachineMaster"></param>
		/// <param name="pdstDetailData"></param>
		//[AutoComplete(true)]
		public void UpdateMachineTime(object pobjMachineMaster, DataSet pdstDetailData)
		{
			try
			{
				//add the master first
				PRO_MachineTimeMasterDS objPRO_MachineTimeMasterDS = new PRO_MachineTimeMasterDS();
				objPRO_MachineTimeMasterDS.Update(pobjMachineMaster);
			
				//update the detail
				PRO_MachineTimeDetailDS objPRO_MachineTimeDetailDS = new PRO_MachineTimeDetailDS();
				objPRO_MachineTimeDetailDS.UpdateDataSet(pdstDetailData);
			}			
			catch (PCSDBException ex)
			{
				throw ex;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		#endregion
	}
}
